/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdminMainPanel.java
 *
 * Created on 2009-9-24, 0:44:41
 */
package com.res0w.SMMS.ui.admin;

import com.res0w.SMMS.ui.UDResource.ImageButton;
import com.res0w.SMMS.ui.UDResource.ListMenuWindow;
import com.res0w.SMMS.ui.admin.MenuList.GameStatisticsListMenu;
import com.res0w.SMMS.ui.admin.MenuList.GamingListMenu;
import com.res0w.SMMS.ui.admin.MenuList.PreGameListMenu;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.AbstractButton;
import javax.swing.SwingUtilities;

/**
 *
 * @author res0w
 * @since 2009-9-24
 * @version 0.3
 *
 */
public class AdminMainPanel extends javax.swing.JPanel {

    /** Creates new form AdminMainPanel */
    public AdminMainPanel() {
        initComponents();
        PanelInit();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jMenuBar2 = new javax.swing.JMenuBar();
        jMenu3 = new javax.swing.JMenu();
        jMenu4 = new javax.swing.JMenu();
        jComboBox1 = new javax.swing.JComboBox();
        jMenuBar3 = new javax.swing.JMenuBar();
        jMenu5 = new javax.swing.JMenu();
        jMenu6 = new javax.swing.JMenu();
        jPopupMenu1 = new javax.swing.JPopupMenu();

        jButton1.setText("jButton1");

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        jMenu3.setText("File");
        jMenuBar2.add(jMenu3);

        jMenu4.setText("Edit");
        jMenuBar2.add(jMenu4);

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jMenu5.setText("File");
        jMenuBar3.add(jMenu5);

        jMenu6.setText("Edit");
        jMenuBar3.add(jMenu6);

        setBackground(new java.awt.Color(255, 255, 255));
        setPreferredSize(new java.awt.Dimension(1059, 732));
        setRequestFocusEnabled(false);
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                formMousePressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1059, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 732, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void formMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMousePressed
        // TODO add your handling code here:
        pressedPoint = evt.getLocationOnScreen();
    }//GEN-LAST:event_formMousePressed
    private Point pressedPoint;

    private void formMouseDragged(java.awt.event.MouseEvent evt) {
        Point draggedPoint = evt.getLocationOnScreen();
        Point location = SwingUtilities.getRoot(this).getLocationOnScreen();
        int x = draggedPoint.x - pressedPoint.x;
        int y = draggedPoint.y - pressedPoint.y;
        SwingUtilities.getRoot(this).setLocation(location.x + x, location.y + y);
        pressedPoint = draggedPoint;
        //当鼠标在拖拽的时候如果listMenu开启，则关闭；
        setListMenuVisible(false);
    }
    private BufferedImage bg;
    ImageButton btn1 = new ImageButton("/com/res0w/SMMS/ui/UDResource/b1_d.png",
            "/com/res0w/SMMS/ui/UDResource/b1_h.png",
            "/com/res0w/SMMS/ui/UDResource/b1_h.png");
    ImageButton btn2 = new ImageButton("/com/res0w/SMMS/ui/UDResource/b2_d.png",
            "/com/res0w/SMMS/ui/UDResource/b2_h.png",
            "/com/res0w/SMMS/ui/UDResource/b2_h.png");
    ImageButton btn3 = new ImageButton("/com/res0w/SMMS/ui/UDResource/b3_d.png",
            "/com/res0w/SMMS/ui/UDResource/b3_h.png",
            "/com/res0w/SMMS/ui/UDResource/b3_h.png");
    ImageButton btn4 = new ImageButton("/com/res0w/SMMS/ui/UDResource/bclose_d.png",
            "/com/res0w/SMMS/ui/UDResource/bclose_h.png",
            "/com/res0w/SMMS/ui/UDResource/bclose_h.png");

    private void PanelInit() {
        //监控单击事件，当监听到界面有单机的时候关闭LISTMENU
        addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                setListMenuVisible(false);
            }
        });
        addMouseMotionListener(new MouseAdapter() {

            @Override
            public void mouseDragged(MouseEvent e) {
                formMouseDragged(e);
            }
        });
        try {
            bg = ImageIO.read(getClass().getResource("/com/res0w/SMMS/ui/admin/MainFrame1.png"));
        } catch (IOException ex) {
        }
        this.setPreferredSize(new Dimension(bg.getWidth(), bg.getHeight()));

        setButtonLocation(btn1, 106, 51);
        setButtonLocation(btn2, 245, 51);
        setButtonLocation(btn3, 384, 51);
        setButtonLocation(btn4, 522, 66);

        btn1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                popMenuActionListener(pglm, btn1, 106, 51);
            }
        });
        btn2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                popMenuActionListener(glm, btn2, 245, 51);
            }
        });
        btn3.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                popMenuActionListener(gslm, btn3, 384, 51);
            }
        });
        btn4.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        addSubPanel();
    }
//    private ListMenuWindow listMenu;
    private PreGameListMenu pglm;
    private GamingListMenu glm;
    private GameStatisticsListMenu gslm;
    //
    public GameSettingPanel gsp;
    public GameScheduleSettingPanel gssp;
    public GameSignUpGPanel gsugp;
    public GameRollCallPanel grcp;
    public GameResultInputPanel grip;
    public GameStatisticsPanel gstp;

    public void setSubPanelVisible(boolean isVisible) {
        gsp.setVisible(isVisible);
        gssp.setVisible(isVisible);
        grcp.setVisible(isVisible);
        grip.setVisible(isVisible);
        gstp.setVisible(isVisible);
        gsugp.setVisible(isVisible);
    }

    private void setListMenuVisible(boolean isVisible) {
        pglm.setVisible(isVisible);
        glm.setVisible(isVisible);
        gslm.setVisible(isVisible);
    }

    private void setListMenuVisible(ListMenuWindow listMenu) {
        setListMenuVisible(false);
        listMenu.setVisible(true);
    }

    private void popMenuActionListener(ListMenuWindow listMenu,
            ImageButton btn, int x, int y) {
        Point location = SwingUtilities.getRoot(this).getLocationOnScreen();
        y = location.y + btn.getSize().height + y;
        x = location.x + x;
        listMenu.setLocation(x, y);
        setListMenuVisible(listMenu);
    }

    private void addSubPanel() {
        gsp = new GameSettingPanel();
        gsp.setBounds(50, 140, 950, 530);
        this.add(gsp);
        gssp = new GameScheduleSettingPanel();
        gssp.setBounds(50, 140, 950, 570);
        this.add(gssp);
        gsugp = new GameSignUpGPanel();
        gsugp.setBounds(50, 140, 950, 530);
        this.add(gsugp);
        grcp = new GameRollCallPanel();
        grcp.setBounds(50, 140, 950, 530);
        this.add(grcp);
        grip = new GameResultInputPanel();
        grip.setBounds(50, 140, 950, 530);
        this.add(grip);
        gstp = new GameStatisticsPanel();
        gstp.setBounds(50, 140, 950, 530);
        this.add(gstp);
        setSubPanelVisible(false);

        pglm = new PreGameListMenu(this);
        glm = new GamingListMenu(this);
        gslm = new GameStatisticsListMenu(this);
        setListMenuVisible(false);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(bg, 0, 0, this);
    }

    private void setButtonLocation(AbstractButton ab, int x, int y) {
        int width = ab.getIcon().getIconWidth();
        int height = ab.getIcon().getIconHeight();
        ab.setBounds(x, y, width, height);
        this.add(ab);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenu jMenu5;
    private javax.swing.JMenu jMenu6;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuBar jMenuBar2;
    private javax.swing.JMenuBar jMenuBar3;
    private javax.swing.JPopupMenu jPopupMenu1;
    // End of variables declaration//GEN-END:variables
}
